﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Bam
{
    public abstract class Menu
    {
        public Dictionary<String, int> items;
        public int line_height;
        public Vector2 pos;

        public int selected_index = 0;

        public bool cylce = false;
        Input active_input;
        public Menu()
        {
            
        }
        public virtual void render()
        {
            int count =0;
            foreach (KeyValuePair<String,int> i in items)
            {
                int lh = globals.default_font.LineSpacing;
                
                if (selected_index != count) 
                    globals.drawText(i.Key,pos.X,pos.Y+(count*(lh+line_height)),Color.Black);
                else
                    globals.drawText(" "+i.Key, pos.X, pos.Y + (count * (lh + line_height)), Color.Gold);
            
                count++;
            }
            
        }
        public virtual bool handle_input()
        {
            if (active_input == null)
                return false;

            
            if (active_input.is_pressed(BUTTON_ACTION.down) == true)
            {
                selected_index++;
                if (selected_index >= items.Count - 1)
                {
                    if (cylce == true)
                        selected_index = 0;
                    else
                        selected_index = items.Count - 1;
                }
            }
            if (active_input.is_pressed(BUTTON_ACTION.up) == true)
            {
                selected_index--;
                if (selected_index < 0)
                {
                    if (cylce == true)
                        selected_index = items.Count - 1;
                    else
                        selected_index = 0;
                }
            }
            active_input.update();
            return active_input.is_pressed(BUTTON_ACTION.fire);
        }
        public void set_active_input(Input ai)
        {
            active_input = ai;
        }
        public virtual int get_selected_value()
        {
            return items.ElementAt(selected_index).Value;
        }
        public virtual String get_selected_key()
        {
            return items.ElementAt(selected_index).Key;
        }
    }
    
}
